/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package tablasSql;

import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.table.DefaultTableModel;
import otros.MiTableModel;

/**
 *
 * @author FERRETERIA
*/

public class Proveedor {
    
    public static MiTableModel getProveedores(String criterio, String dato) {
        String[] columNames = {"Codigo","NIF / DNI", "Nombre Comercial", "Nombre Fiscal", "Telefono"}; 
        String[] tableNames = {"Codigo","nif","nombrecomercial", "nombrefiscal",  "telefono1"};
        Class [] tipos = {Integer.class, String.class, String.class, String.class, String.class}; 
                    
        String sql = "SELECT * from nProveedores";
        String contar = "nproveedores";
        String aux = "";
        if (!dato.isEmpty()) {
            if (criterio.compareTo("codigo") == 0) 
                aux += " where " + criterio + " like '" + dato + "'";
            else
                aux += " where " + criterio + " like '%" + dato + "%'";     
        }
        sql += aux;
        contar += aux;
        
        Object[][] dtDatos = new Sql().GetTabla(tableNames, contar, sql, tipos);
        if (dtDatos.length > 0) 
            return new MiTableModel(dtDatos,columNames);        
        else
            return null;
        
    }

    
    public static ResultSet getProveedor (String cod) {
        String sql = "Select * from nproveedores where codigo = '" + cod + "'";
        return new Sql().getDatos(sql);
    }
    
    public static void guardar (String [] datos, boolean guardar) {
        String sql = "";   
        int numString = 0;
        int numFloat = 0;
        if (!guardar) {
           sql = "update nproveedores set nombrecomercial = ?, nombrefiscal = ?, domicilio = ?,";
           sql += "localidad = ?, provincia = ?, codigopostal = ?, nif = ?, telefono1 = ?, telefono2 = ?,";
           sql += "email = ?, web = ?, fax = ?, tipoPago = ? where codigo = ?";
           numString = 12;
           
        }
        else {
            sql = "insert into nproveedores (nombrecomercial,nombrefiscal,domicilio," +
                        "localidad, provincia, codigopostal,nif,telefono1,telefono2,email, web,fax,creado,tipoPago," +
                    "Codigo) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
            numString = 14;
        }
        new Sql().actualizarDatos(sql, datos, numString, numFloat);
    }
    
    public static void borrar(String id) {
        String sql = "delete from nproveedores where codigo = " + id;
        new Sql().hacerConsulta(sql);
    }
    
    public static int getCantFac(int id, int tipo) {
        String sql = "select count(*) as cantidad from compras where idprove = " + id + " and tipo = " + tipo;
        ResultSet re = new Sql().getDatos(sql);
        try {
            if (re.next())
                return re.getInt("cantidad");
        } catch (SQLException ex) {
            System.out.println("Proveedor.getCantFac: " + ex);
            return 0;
        }
        return 0;
    }
    
    public static void guardarUltTicket(String[]datos, int tipo) {
        String campo = "";
        switch (tipo) {
            case 0:
                campo = "idultfac";
                break;
            case 1:
                campo = "idultalb";
                break;
            case 2:
                campo = "idultabono";
                break;
        }
        String sql = "update nproveedores set " + campo + " = ? where codigo = ?";
        new Sql().actualizarDatos(sql, datos, 1, 0);
    }
    
    public static Boolean buscarProveeEnFacturas(String id) {
        String sql = "select * from compras where idprove="+id;
        ResultSet re = new Sql().getDatos(sql);
        try {
            if (re.next()) {
                return true;
            }
        } catch (SQLException ex) {
            System.out.println("Clientes.getCantFac: " + ex);
        }
        return false;
    }
}
